package color.koitq.picker.util;

import android.content.res.AssetManager;
import android.graphics.Color;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Random;

import color.koitq.picker.App;

public class Util {

    public static String getJson(String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = App.getContext().getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public static int getRandomInt(int start, int range) {
        Random random = new Random();
        return random.nextInt(range - start) + start;
    }

    public static double getScore(double valueOne, double valueTwo) {
        BigDecimal b1 = new BigDecimal(java.lang.Double.toString(valueOne));
        BigDecimal b2 = new BigDecimal(java.lang.Double.toString(valueTwo));
        return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static String getStringFromAssets(String fileName) {
        StringBuffer sb = new StringBuffer();
        try {
            InputStreamReader inputReader = new InputStreamReader(App.getContext().getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            while ((line = bufReader.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    public static ArrayList<String> getStringListFromAssets(String fileName) {
        ArrayList<String> list = new ArrayList<>();
        try {
            InputStreamReader inputReader = new InputStreamReader(App.getContext().getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            while ((line = bufReader.readLine()) != null) {
                list.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    public static String getRandomColor() {
        Random random = new Random();

// create a big random number - maximum is ffffff (hex) = 16777215 (dez)

        int nextInt = random.nextInt(0xffffff + 1);

// format it as hexadecimal string (with hashtag and leading zeros)

        String colorCode = String.format("#%06x", nextInt);
        return colorCode;
    }
    public static String rgbToHex(int r ,int g ,int b){
        String str = String.format("#%02x%02x%02x",r,g,b);
        return str;
    }
}
